package com.example.ycq.controller;

import com.example.ycq.pojo.User;
import com.example.ycq.service.MyBatisUserService;
import com.example.ycq.service.UserBatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class MyBatisController {

    @Autowired
    private MyBatisUserService myBatisUserService=null;
    @Autowired
    private UserBatchService userBatchService=null;
    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @RequestMapping("/getUser")
    public User getUser(Long id){
        return myBatisUserService.getUser(id);
    }

    /**
     * 插入用户信息
     * @param userName
     * @param note
     * @return
     */
    @RequestMapping("/insertUser")
    public Map<String,Object> insertUser(String userName,String note){
            User user=new User();
            user.setUserName(userName);
            user.setNote(note);
            //结果回填主键，返回插入条数
            int  update= myBatisUserService.insertUser(user);
            Map<String,Object> result=new HashMap<>();
            result.put("success",update==1);
            result.put("user",user);
            return  result;
    }

    /**
     * 批量插入
     * @param userName1
     * @param note1
     * @param userName2
     * @param note2
     * @return
     */
    @RequestMapping("/insertUsers")
    public Map<String,Object> insertUsers(String userName1,String note1,String userName2,String note2){
        User user1=new User();
        user1.setUserName(userName1);
        user1.setNote(note1);
        User user2=new User();
        user2.setUserName(userName2);
        user2.setNote(note2);
        List<User> userList=new ArrayList<>();
        userList.add(user1);
        userList.add(user2);
        //结果会回填主键，返回插入条数
        int inserts=userBatchService.insertUsers(userList);
        Map<String,Object> result=new HashMap<>();
        result.put("success",inserts>0);
        result.put("user",userList);
        return  result;



    }
}
